package zadanie4;
//import graph.test;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.util.ArrayList;
import java.util.stream.IntStream;

import javax.swing.JLabel;
import javax.swing.JSpinner;

/*import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;*/


import zadanie4.Zadanie.MyThread;

import java.awt.*;


public class Window implements onCountingComplete {
	
	Functions functions = new Functions();
	Zadanie zadanie ;
	TextArea textArea;
	JLabel lblKomunikaty;

	private JFrame frame;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Window window = new Window();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});		
	}

	/**
	 * Create the application.
	 */
	public Window() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
	
		
		lblKomunikaty = new JLabel("Komunikaty");
		lblKomunikaty.setBounds(175, 17, 249, 30);
		frame.getContentPane().add(lblKomunikaty);
		
		
		JButton Button_generate_data = new JButton("generuj dane");
		Button_generate_data.setBounds(275, 228, 149, 23);
		Button_generate_data.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				functions.generateList();
				//zadanie = new Zadanie(functions.generateList());
				lblKomunikaty.setText("dane wygenerowano");
			}
		});
		frame.getContentPane().add(Button_generate_data);
		
		JButton btnWczytajDane = new JButton("wczytaj dane");
		btnWczytajDane.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<Integer> arrayList =  functions.loadDatafromFile();
				zadanie = new Zadanie(arrayList,Window.this);

			}
		});
		btnWczytajDane.setBounds(41, 228, 120, 23);
		frame.getContentPane().add(btnWczytajDane);
		
		JSpinner spinner_thread_number = new JSpinner(); // generyczny kontruktor 
		spinner_thread_number.setBounds(10, 46, 45, 20);
		frame.getContentPane().add(spinner_thread_number);
		
		JLabel lblIloWtkw = new JLabel("Ilo\u015B\u0107 w\u0105tk\u00F3w");
		lblIloWtkw.setBounds(10, 25, 67, 14);
		frame.getContentPane().add(lblIloWtkw);
		
		textArea = new TextArea();
		textArea.setBounds(155, 68, 269, 154);
		frame.getContentPane().add(textArea);
		
		JButton btnStart = new JButton("Start");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lblKomunikaty.setText("Czekaj.. trwa obliczanie histogramu");
				zadanie.start((int) spinner_thread_number.getValue());
			}
		});
		btnStart.setBounds(41, 114, 108, 72);
		frame.getContentPane().add(btnStart);
		

		

		

	}

	@Override
	public void CountingComplete() {
		// TODO Auto-generated method stub
		lblKomunikaty.setText("Zadanie wykonane ");
		int[] table = zadanie.getTable();
		int suma = 0;
		for(int i = 0 ; i < 256; i ++){
			textArea.append(i + ": " + table[i] + " \n");
			suma += table[i];
		}
		textArea.append("Suma wszystkich to :"+suma + " \n");
		textArea.append("Czas obliczania: "+ zadanie.getCountingTime() +"ms"+ " \n");
		
		textArea.append("Czas wykonania watkow :\n");
		ArrayList<MyThread> myThreads = zadanie.getMyThreads();
		for(int i = 0 ; i < myThreads.size();i++){
			textArea.append("Watek "+i + " "+ myThreads.get(i).getThreadCountingTime() + "ms \n");
		}
	}
	
}
